package com.itany.book.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itany.constants.Constants;
import com.itany.exception.BookException;
import com.itany.mapper.BookMapper;
import com.itany.pojo.Book;
import com.itany.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
@Transactional(propagation = Propagation.REQUIRED,rollbackFor = Throwable.class)
public class BookServiceImpl implements BookService {

    @Autowired
    private BookMapper bookMapper;


    @Override
    public PageInfo<Book> findAll(String pageNum, String pageSize, Book book) {

        if (pageNum==null || "".equals(pageNum.trim())){
            pageNum= Constants.PAGE_NUM+"";

        }
        if (pageSize==null ||"".equals(pageSize.trim())){
            pageSize=Constants.PAGE_SIZE+"";
        }
        if (null!=book.getPressId() && book.getPressId()==-1){
            book.setPressId(null);
        }
        if (null!=book.getSupplierId() && book.getSupplierId()==-1){
            book.setSupplierId(null);
        }
        PageHelper.startPage(Integer.parseInt(pageNum),Integer.parseInt(pageSize));

        List<Book>books=bookMapper.selectAll(book);
        PageInfo<Book>pageInfo=new PageInfo<>(books);
        return pageInfo;
    }

    @Override
    public void modifyBookStatus(Book book) throws BookException {
        try {
            bookMapper.updateBookStatus(book);
        } catch (Exception e) {
            throw new BookException("服务器异常修改失败");
        }


    }
}
